package org.devgframwork.platform.model;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;

import java.io.Serializable;
import java.util.List;

/**
 * @Description
 * @auther dh
 * @date 2022/7/13 16:21
 */
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    private long totalCount = 0;
    private int pageSize = 1;
    private int pageIndex = 1;
    private List<T> list;

    public static <T> PageResult<T> build(List<T> data) {
        PageInfo pageInfo = new PageInfo(data);
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setList(pageInfo.getList());
        pageResult.setPageIndex(pageInfo.getPageNum());
        pageResult.setPageSize(pageInfo.getPageSize());
        pageResult.setTotalCount(pageInfo.getTotal());
        return pageResult;

    }

    public static <T> PageResult<T> build(Page<T> data) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setList(data.getRecords());
        pageResult.setPageIndex((int) data.getCurrent());
        pageResult.setPageSize((int) data.getPages());
        pageResult.setTotalCount(data.getTotal());
        return pageResult;

    }

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}
